package com.baseandroid.jpush;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import com.qianbao.qianbaobusiness.R;

/*--建立前台服务,保障后台程序也能收到推送--*/
public class JPushService extends Service {

    @Override
    public void onCreate() {
        NotificationCompat.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("jpush_foreground_service", "语音播报前台服务", NotificationManager.IMPORTANCE_MIN);
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(this, "jpush_foreground_service");
        } else {
            /*---构建Notification---*/
            builder = new NotificationCompat.Builder(this, null);
        }

        builder.setSmallIcon(android.R.drawable.stat_notify_chat);
        builder.setContentTitle("钱包商家推送服务");
        builder.setContentText("请勿关闭，保证程序后台语音播报");
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSound(null);
        builder.setVibrate(null);
        builder.setLights(0, 0, 0);
        builder.setOngoing(true);
        //builder.setWhen(xxx); //通知时间显示
        //builder.setContentInfo("xxx");//通知右下角显示
        //builder.setAutoCancel(false)//设置为true,点击该条通知会自动删除,false时只能通过滑动来删除
        //builder.setOngoing()//设置是否为一个正在进行中的通知，这一类型的通知将无法删除
        //builder.setContentIntent(PendingIntent intent)//设置点击通知后操作
        //builder.setDefaults(int defaults)//设置铃声,振动,闪烁用|分隔，常量在Notification里
        Notification notification = builder.build();
        //---发送通知---
        //NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        //notificationManagerCompat.notify(0x1234, builder.build());
        startForeground(1, notification);//启动前台服务
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (Build.VERSION.SDK_INT >= 18) {
            Intent innerIntent = new Intent(this, JPushInnerService.class);
            startService(innerIntent);
        }
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        stopForeground(true);//停止前台服务
    }


    public static class JPushInnerService extends Service {

        @Override
        public void onCreate() {
            NotificationCompat.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel("jpush_foreground_service", "语音播报前台服务", NotificationManager.IMPORTANCE_MIN);
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.createNotificationChannel(channel);
                builder = new NotificationCompat.Builder(this, "jpush_foreground_service");
            } else {
                /*---构建Notification---*/
                builder = new NotificationCompat.Builder(this, null);
            }

            builder.setSmallIcon(android.R.drawable.stat_notify_chat);
            builder.setContentTitle("钱包商家推送服务");
            builder.setContentText("请勿关闭，保证程序后台语音播报");
            builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
            builder.setSound(null);
            builder.setVibrate(null);
            builder.setLights(0, 0, 0);
            builder.setOngoing(true);
            //builder.setWhen(xxx); //通知时间显示
            //builder.setContentInfo("xxx");//通知右下角显示
            //builder.setAutoCancel(false)//设置为true,点击该条通知会自动删除,false时只能通过滑动来删除
            //builder.setOngoing()//设置是否为一个正在进行中的通知，这一类型的通知将无法删除
            //builder.setContentIntent(PendingIntent intent)//设置点击通知后操作
            //builder.setDefaults(int defaults)//设置铃声,振动,闪烁用|分隔，常量在Notification里
            Notification notification = builder.build();
            //---发送通知---
            //NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
            //notificationManagerCompat.notify(0x1234, builder.build());
            startForeground(1, notification);//启动前台服务
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    }

}